<?
  require "include/bittorrent.php";
  dbconn(false);

  loggedinorreturn();

  $searchstr = unesc($_GET["search"]);
  $search = searchfield($searchstr);
  if (empty($search))
  unset($search);

  //delete items older than a week
  $secs = 7 * 24 * 60 * 60;
  stdhead("Site log");
  query("DELETE FROM sitelog WHERE " . time() . " - UNIX_TIMESTAMP(added) > $secs") or sqlerr(__FILE__, __LINE__);


  print("<h1>Site log</h1>\n");


  print("<p><form method=get action=?>\n");
  print("Search: <input type=text size=40 name=search value=\"" . htmlspecialchars($searchstr) . "\">\n");
  print("<input type=submit value='Okay'>\n");
  print("</form></p>\n");


if (isset($search))
  $res = query("SELECT added, txt FROM sitelog WHERE txt LIKE " . sqlesc("%$search%") . " ORDER BY added DESC") or sqlerr(__FILE__, __LINE__);
else
  $res = query("SELECT added, txt FROM sitelog ORDER BY added DESC") or sqlerr(__FILE__, __LINE__);


  if (mysql_num_rows($res) == 0)
  {
    if (isset($search))
	print("<b>No Search Results</b>\n");
    else
	print("<b>Log is empty</b>\n");
  }
  else
  {
    print("<table border=1 cellspacing=0 cellpadding=5>\n");
    print("<tr><td class=colhead align=left>Date</td><td class=colhead align=left>Time</td><td class=colhead align=left>Event</td></tr>\n");
    while ($arr = mysql_fetch_assoc($res))
    {
      $color = 'white'; 
      if (strpos($arr['txt'],'was uploaded by')) 
      $color = "#96FF96";
      if (strpos($arr['txt'],'was edited by')) 
      $color = "#FFFF96";
      if (strpos($arr['txt'],'was deleted by')) 
      $color = "#FF9696";

      $date = substr($arr['added'], 0, strpos($arr['added'], " "));
      $time = substr($arr['added'], strpos($arr['added'], " ") + 1);
      print("<tr><td style=background-color:$color>$date</td><td style=background-color:$color>$time</td><td style=background-color:$color align=left>$arr[txt]</td></tr>\n");
    }
    print("</table>");
  }
  print("<p>Times are in PST.</p>\n");
  stdfoot();
  
?>